package com.ex.orage2app;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.SearchView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ProductFragment extends Fragment implements View.OnClickListener{

    private GridView gridView;
    private List<Product> productList;
    private ProductAdapter productAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.content_product, container, false);
        init(view);
        return view;
    }
    /**
     * 初始化组件
     */
    private void init(View view) {
        //内容布局实现
        gridView = view.findViewById(R.id.index_famous_gridview);
        initData();
        productAdapter = new ProductAdapter(getActivity(), productList);
        gridView.setAdapter(productAdapter);
        //gridview的每个的监听
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=null;
                switch (position){
                    case 0:
                        intent=new Intent(getActivity(),ProductdetailActivity.class);
                        //传输数据
                        String productname=productList.get(position).getProductName();
                        BigDecimal productprice=productList.get(position).getProductPrice();
                        Integer productimg=productList.get(position).getImageUrlId();
                        Bundle bundle=new Bundle();
                        bundle.putString("productname",productname);
                        bundle.putString("productprice",productprice.toString());
                        bundle.putInt("productimg",productimg);
                        Log.i("1",productname);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        break;

                }
            }
        });
    }
    /**
     * 初始化商品数据
     */
    private void initData() {
        productList = new ArrayList<>();
        Product product = new Product();
        product.setImageUrlId(R.drawable.fanruowei);
        product.setProductName("梵诺威");
        product.setProductPrice(new BigDecimal("299.9"));
        Product product1 = new Product();
        product1.setImageUrlId(R.drawable.aj1);
        product1.setProductName("AJ1");
        product1.setProductPrice(new BigDecimal("899.9"));
        Product product2 = new Product();
        product2.setImageUrlId(R.drawable.simpsons);
        product2.setProductName("辛普森");
        product2.setProductPrice(new BigDecimal("199.9"));
        Product product3 = new Product();
        product3.setImageUrlId(R.drawable.dunk);
        product3.setProductName("Dunk");
        product3.setProductPrice(new BigDecimal("459.9"));
        Product product4 = new Product();
        product4.setImageUrlId(R.drawable.jimmy);
        product4.setProductName("Jimmy");
        product4.setProductPrice(new BigDecimal("2999.9"));
        Product product5 = new Product();
        product5.setImageUrlId(R.drawable.lilywei);
        product5.setProductName("Lily Wei");
        product5.setProductPrice(new BigDecimal("499.9"));
        Product product6 = new Product();
        product6.setImageUrlId(R.drawable.qipange);
        product6.setProductName("棋盘格");
        product6.setProductPrice(new BigDecimal("269.9"));
        Product product7 = new Product();
        product7.setImageUrlId(R.drawable.feidian3);
        product7.setProductName("跑鞋");
        product7.setProductPrice(new BigDecimal("499.9"));
        productList.add(product);
        productList.add(product1);
        productList.add(product2);
        productList.add(product3);
        productList.add(product4);
        productList.add(product5);
        productList.add(product6);
        productList.add(product7);

    }


    @Override
    public void onClick(View v) {


    }
}
